package dao;

import entity.FilmInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import tool.MyBatisUtil;

import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class FilmInfoDaoImpl implements  FilmInfoDao {

    SqlSessionFactoryBuilder builder=null;
    SqlSessionFactory factory=null;
    SqlSession sqlSession=null;
    //输入流读取配置文件
    Reader reader=null;
    public List<FilmInfo> getFilmInfos() {
        List<FilmInfo> list=new ArrayList<FilmInfo>();
        try{
            //读取配置文件
            reader= Resources.getResourceAsReader("mybatis-config.xml");
            //构键builder
            builder=new SqlSessionFactoryBuilder();
            //构键factory
            factory=builder.build(reader);
            //构键sqlSession
            sqlSession=factory.openSession();
            //执行数据库的操作dao接口的全限定名+id名
            //list=sqlSession.selectList("dao.FilmInfoDao.findfilminfo");
            list=sqlSession.getMapper(FilmInfoDao.class).getFilmInfos();
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            //释放资源
            sqlSession.close();
        }
    }

    @Override
    public List<FilmInfo> getFilmInfosByName(String filmname) {
        List<FilmInfo> list=new ArrayList<FilmInfo>();
        try{
            //读取配置文件
            reader= Resources.getResourceAsReader("mybatis-config.xml");
            //构键builder
            builder=new SqlSessionFactoryBuilder();
            //构键factory
            factory=builder.build(reader);
            //构键sqlSession
            sqlSession=factory.openSession();
            //执行数据库的操作dao接口的全限定名+id名
            //list=sqlSession.selectList("dao.FilmInfoDao.findfilminfo");
            list=sqlSession.getMapper(FilmInfoDao.class).getFilmInfosByName(filmname);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            //释放资源
            sqlSession.close();
        }
    }

    @Override
    public List<FilmInfo> getFilmInfosByCondition(String filmname, Integer money) {
        List<FilmInfo> list=new ArrayList<FilmInfo>();
        try{
            //构键sqlSession
            sqlSession= MyBatisUtil.getSession();
            //执行数据库的操作dao接口的全限定名+id名
            //list=sqlSession.selectList("dao.FilmInfoDao.findfilminfo");
            list=sqlSession.getMapper(FilmInfoDao.class).getFilmInfosByCondition(filmname,money);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            //释放资源
            sqlSession.close();
        }
    }

    @Override
    public List<FilmInfo> getFilmInfosByCondition2(Map<String, Object> map) {
        List<FilmInfo> list=new ArrayList<FilmInfo>();
        try{
            //构键sqlSession
            sqlSession= MyBatisUtil.getSession();
            //执行数据库的操作dao接口的全限定名+id名
            //list=sqlSession.selectList("dao.FilmInfoDao.findfilminfo");
            list=sqlSession.getMapper(FilmInfoDao.class).getFilmInfosByCondition2(map);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            //释放资源
            sqlSession.close();
        }
    }

    @Override
    public List<FilmInfo> getFilmInfosByCondition3(FilmInfo info) {
        List<FilmInfo> list=new ArrayList<FilmInfo>();
        try{
            //构键sqlSession
            sqlSession= MyBatisUtil.getSession();
            //执行数据库的操作dao接口的全限定名+id名
            //list=sqlSession.selectList("dao.FilmInfoDao.findfilminfo");
            list=sqlSession.getMapper(FilmInfoDao.class).getFilmInfosByCondition3(info);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            //释放资源
            sqlSession.close();
        }
    }

    @Override
    public List<FilmInfo> getFilmInfosByCondition4(FilmInfo info) {
        List<FilmInfo> list=new ArrayList<FilmInfo>();
        try{
            //构键sqlSession
            sqlSession= MyBatisUtil.getSession();
            //执行数据库的操作dao接口的全限定名+id名
            //list=sqlSession.selectList("dao.FilmInfoDao.findfilminfo");
            list=sqlSession.getMapper(FilmInfoDao.class).getFilmInfosByCondition4(info);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            //释放资源
            sqlSession.close();
        }
    }

    @Override
    public List<FilmInfo> getFilmInfosByCondition5(String choice) {
        List<FilmInfo> list=new ArrayList<FilmInfo>();
        try{
            //构键sqlSession
            sqlSession= MyBatisUtil.getSession();
            //执行数据库的操作dao接口的全限定名+id名
            //list=sqlSession.selectList("dao.FilmInfoDao.findfilminfo");
            list=sqlSession.getMapper(FilmInfoDao.class).getFilmInfosByCondition5(choice);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            //释放资源
            sqlSession.close();
        }
    }

    @Override
    public List<FilmInfo> getFilmInfosByCondition6(Integer[] typeid) {
        List<FilmInfo> list=new ArrayList<FilmInfo>();
        try{
            //构键sqlSession
            sqlSession= MyBatisUtil.getSession();
            //执行数据库的操作dao接口的全限定名+id名
            //list=sqlSession.selectList("dao.FilmInfoDao.findfilminfo");
            list=sqlSession.getMapper(FilmInfoDao.class).getFilmInfosByCondition6(typeid);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            //释放资源
            sqlSession.close();
        }
    }

    @Override
    public List<FilmInfo> getFilmInfosByCondition7(List<Integer> typeid) {
        List<FilmInfo> list=new ArrayList<FilmInfo>();
        try{
            //构键sqlSession
            sqlSession= MyBatisUtil.getSession();
            //执行数据库的操作dao接口的全限定名+id名
            //list=sqlSession.selectList("dao.FilmInfoDao.findfilminfo");
            list=sqlSession.getMapper(FilmInfoDao.class).getFilmInfosByCondition7(typeid);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            //释放资源
            sqlSession.close();
        }
    }

    @Override
    public List<FilmInfo> getFilmInfosAndType() {
        List<FilmInfo> list=new ArrayList<FilmInfo>();
        try{
            //构键sqlSession
            sqlSession= MyBatisUtil.getSession();
            //执行数据库的操作dao接口的全限定名+id名
            //list=sqlSession.selectList("dao.FilmInfoDao.findfilminfo");
            list=sqlSession.getMapper(FilmInfoDao.class).getFilmInfosAndType();
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            //释放资源
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public int getFilmCount() {
        int count=0;
        try{
            //构键sqlSession
            sqlSession= MyBatisUtil.getSession();
            //执行数据库的操作dao接口的全限定名+id名
            //list=sqlSession.selectList("dao.FilmInfoDao.findfilminfo");
            count=sqlSession.getMapper(FilmInfoDao.class).getFilmCount();
            return count;
        }catch (Exception e){
            e.printStackTrace();
            return -1;
        }finally {
            //释放资源
            //sqlSession.close();
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public List<FilmInfo> getFilmInfosAndTypeByPage(Integer pageIndex, Integer pageSize) {
        List<FilmInfo> list=new ArrayList<FilmInfo>();
        try{
            //构键sqlSession
            sqlSession= MyBatisUtil.getSession();
            //执行数据库的操作dao接口的全限定名+id名
            //list=sqlSession.selectList("dao.FilmInfoDao.findfilminfo");
            list=sqlSession.getMapper(FilmInfoDao.class).getFilmInfosAndTypeByPage(pageIndex,pageSize);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            //释放资源
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public int getFilmCount2(FilmInfo info) {
        int count=0;
        try{
            //构键sqlSession
            sqlSession= MyBatisUtil.getSession();
            //执行数据库的操作dao接口的全限定名+id名
            //list=sqlSession.selectList("dao.FilmInfoDao.findfilminfo");
            count=sqlSession.getMapper(FilmInfoDao.class).getFilmCount2(info);
            return count;
        }catch (Exception e){
            e.printStackTrace();
            return -1;
        }finally {
            //释放资源
            //sqlSession.close();
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public List<FilmInfo> getFilmInfosAndTypeByPage2(Integer pageIndex, Integer pageSize,FilmInfo info) {
        List<FilmInfo> list=new ArrayList<FilmInfo>();
        try{
            //构键sqlSession
            sqlSession= MyBatisUtil.getSession();
            //执行数据库的操作dao接口的全限定名+id名
            //list=sqlSession.selectList("dao.FilmInfoDao.findfilminfo");
            list=sqlSession.getMapper(FilmInfoDao.class).getFilmInfosAndTypeByPage2(pageIndex,pageSize,info);
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            //释放资源
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public int delFilminfo(Integer filmid) {
        int count=0;
        try{
            //构键sqlSession
            sqlSession= MyBatisUtil.getSession();
            //执行数据库的操作dao接口的全限定名+id名
            //list=sqlSession.selectList("dao.FilmInfoDao.findfilminfo");
            count=sqlSession.getMapper(FilmInfoDao.class).delFilminfo(filmid);
            sqlSession.commit();
            return count;
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            return -1;
        }finally {
            //释放资源
            //sqlSession.close();
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public int addFilminfo(FilmInfo info) {
        int count=0;
        try{
            //构键sqlSession
            sqlSession= MyBatisUtil.getSession();
            //执行数据库的操作dao接口的全限定名+id名
            //list=sqlSession.selectList("dao.FilmInfoDao.findfilminfo");
            count=sqlSession.getMapper(FilmInfoDao.class).addFilminfo(info);
            sqlSession.commit();
            return count;
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            return -1;
        }finally {
            //释放资源
            //sqlSession.close();
            MyBatisUtil.closeSession();
        }
    }

    @Override
    public int updateFilminfo(FilmInfo info) {
        int count=0;
        try{
            //构键sqlSession
            sqlSession= MyBatisUtil.getSession();
            //执行数据库的操作dao接口的全限定名+id名
            //list=sqlSession.selectList("dao.FilmInfoDao.findfilminfo");
            count=sqlSession.getMapper(FilmInfoDao.class).updateFilminfo(info);
            sqlSession.commit();
            return count;
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            return -1;
        }finally {
            //释放资源
            //sqlSession.close();
            MyBatisUtil.closeSession();
        }
    }
}
